<?php

namespace Xrfg\Api\Trading\Account\Resources;

use App\Http\Resources\Resource;
use Xrfg\Core\Common\Services\Proof;
use Xrfg\Core\Trading\Account\Models\Coin;
use Xrfg\Core\Trading\Account\Models\Recharge;
use Xrfg\Core\Trading\Account\Models\Withdraw;

class AccountCapitalflowResource extends Resource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        $withdraw = null;
        if ($this->class_id == 2) {
            $withdraw = Withdraw::find($this->proof_id);
        }

        $recharge = null;
        if ($this->class_id == 1) {
            $recharge = Recharge::find($this->proof_id);
        }

        $coinName = Coin::find($this->coin_id)->name;

        return [
            'id' => $this->id,
            'coin_id' => $this->coin_id,
            'coin_name' => $coinName,
            'user_id' => $this->user_id,
            'op_name' => Proof::getOpName($this->certificate),
            'number' => $this->available_number,
            'time' => $this->created_at,
            'status' => $this->getStatusText($withdraw),
            'address' => ($withdraw || $recharge) ? ($withdraw ? $withdraw->address : $recharge->address) : '',
            'chain_name' => ($withdraw || $recharge) ? $this->getChainName($coinName,$withdraw ? $withdraw->address : $recharge->address) : '',
            'hash' => ($withdraw || $recharge) ? ($withdraw ? $withdraw->hash : $recharge->hash) : '',
            'type' => $this->getType()
        ];
    }

    private function getType()
    {
        switch ($this->class_id) {
            case 1:
                return '充币';
            case 2:
                return '提币';
            case 3:
                return '转入';
            default:
                return $this->class_id;
        }
    }

    private function getStatusText($withdraw)
    {
        if ($withdraw) {
            switch ($withdraw->status) {
                case 0:
                    return '审核中';
                case 1:
                    return '已到账';
                case 2:
                    return '已撤销';
                case 3:
                    return '审核通过未处理';
                case 4:
                    return '处理中';
                default:
                    return $withdraw->status;
            }
        } else {
            return '成功';
        }
    }

    private function getChainName($coin_name, $address)
    {
        switch ($coin_name) {
            case 'USDT':
                if ('0x' == substr($address, 0, 2)) {
                    return 'ERC20';
                } elseif ('T' == substr($address, 0, 1)) {
                    return 'TRC20';
                } else {
                    return 'OMNI';
                }
            case 'BTC':
                return 'bitcoin';
            case 'ETH':
                return 'ethereum';
            case 'EOS':
                return 'eos';
            case 'XRP':
                return 'ripple';
            case 'ripple':
                return 'tron';
            default:
                return '';
        }
    }
}
